package com.example.administrator.gplineview.bitmap_util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

import com.example.administrator.gplineview.MyApplication;

/**
 * Created by chenj
 * Created on 2017/4/24
 */

public class BitmapUtil {
    /**
     * 获得圆角图片的方法
     * @param bm    原始bitmap位图
     * @return      得到的圆角bitmap位图
     */
    public static Bitmap getRoundCornerBitmap(Bitmap bm){
        Bitmap result = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Rect rect = new Rect(0,0,bm.getWidth(),bm.getHeight());
        RectF rectF = new RectF(rect);
        canvas.drawRoundRect(rectF,30,30,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        canvas.drawBitmap(bm,rect,rect,paint);
        return result;
    }

    /**
     * 获得圆形图片的方法
     * @param bm
     * @return
     */
    public static Bitmap getCircleBitmap(Bitmap bm){
        Bitmap result = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
        int radius = Math.min(bm.getWidth(), bm.getHeight()) / 2;
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Rect rect = new Rect(0,0,bm.getWidth(),bm.getHeight());
        canvas.drawCircle(radius,radius,radius,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bm,rect,rect,paint);
        return result;
    }

    /**
     * 获得镜像图片的方法
     * @param bm
     * @return
     */
    public static Bitmap getMirrorBitmap(Bitmap bm){
        Matrix matrix = new Matrix();
        matrix.postScale(1,-1);
        Bitmap temp = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),matrix,false);
        Bitmap result = Bitmap.createBitmap(bm.getWidth(), (int) (bm.getHeight() * 2.1), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(bm,0,0,null);
        canvas.drawBitmap(temp,0,result.getHeight() - bm.getHeight(),null);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new LinearGradient(0,result.getHeight() - bm.getHeight(),0,result.getHeight(),0x90ffffff,0x00ffffff ,Shader.TileMode.MIRROR));
        Rect rect = new Rect(0,result.getHeight() - bm.getHeight(),bm.getWidth(),result.getHeight());
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawRect(rect,paint);
        return result;
    }

    public static Bitmap getBlurBitmap(Bitmap bitmap){
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(MyApplication.getContext());

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

        //Set the radius of the blur
        blurScript.setRadius(10);

        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        //recycle the original bitmap
        bitmap.recycle();

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;
    }
}
